<?php

namespace app\shop\controller;
use app\common\library\HistoryManager;
use think\Controller;
use think\cache\driver\Redis;

class Index extends Controller
{
    //构造函数
    public function __construct()
    {
        parent::__construct();
        //加载模型
        $this->userModel = model('common/User/User');
        $this->bannerModel = model('common/User/Banner');
        $this->shopModel = model('common/User/Shop');
        $this->classShopModel = model('common/User/ClassShop');
        // $this->searchHistoryModel = model('common/User/SearchHistory');
    }
    //轮播图
    public function getBanner(){
      if($this->request->isPost()){
        $bannerResult = $this->bannerModel->where('flag',1)->where('pid',0)->select();
        // halt($bannerResult);
        $data=[
          'banner'=>$bannerResult,
        ];
        $this->success('返回数据成功',null,$data);
        exit;
      }
    }

    //新品-商品
    public function getNewShop(){
      if ($this->request->isPost()) {
        // flag=1是新品品
        $flag = [
          'flag'=>1,
        ];
        $page=$this->request->param('page');
        // halt($page,'66666');
        // $page=2;
        // halt('分类：'.$clValue.'排序：'.$raValue.'搜索：'.$seValue.'页数：'.$page);
        $limit=10;
        $start=($page-1)*$limit;
        $shopResult = $this->shopModel->where($flag)->limit($start,$limit)->select();
        // halt($shopResult);
        
        $shopData=[
          'shop'=>$shopResult,
        ];
        $this->success('返回数据成功',NULL,$shopData);
        exit;
      }
    }

    //获取全部商品
    public function getAllShop(){
      if ($this->request->isPost()) {
        $shopResult = $this->shopModel->select();
        $this->success('返回数据成功',NULL,$shopResult);
        exit;
      }
    }
    //查询分类接口和排序
    public function getClassShop(){
      if ($this->request->isPost()) {
        $clValue=$this->request->param('clValue','0');
        $raValue=$this->request->param('raValue','0');
        $seValue=$this->request->param('seValue','');
        $page=$this->request->param('page','');
        // $page=2;
        // halt('分类：'.$clValue.'排序：'.$raValue.'搜索：'.$seValue.'页数：'.$page);
        $limit=10;
        $start=($page-1)*$limit;
        // halt($seValue);
        if($clValue==0){
          $query=$this->shopModel;
        }else{
          $query=$this->shopModel->where('flag',$clValue);
        }
        if($seValue!==''){
          $query->where('content', 'like', '%' . $seValue . '%');
        }
        switch ($raValue) {
          case 1:
            $query->order('price DESC'); // 价格降序
            break;
          case 2:
            $query->order('stock DESC'); // 库存降序（假设库存字段为stock）
            break;
          default:
            $query->order('id ASC'); // 默认排序，id升序
            break;
        }
        $result = $query->limit($start,$limit)->select();
        $this->success('返回数据成功', null, $result);
        exit;
      }
    }

    // 搜索记录
    public function searchKey(){
      if($this->request->isPost()){
        // $Redis=new Redis();
      // // $Redis->set('aaa','2025/5/22');
        $uid = $this->request->param('uid','','trim');
        $skey = $this->request->param('skey','','trim');
        $stime = (int)time();
        // halt($stime);
        $historyManager = new HistoryManager();
        // 我有设置一天后过期，所以不要的话要去HisotyManager那里把设置过期时间删了
        $result = $historyManager->add($uid, $skey, $stime);
        $getResult = $historyManager->getAll($uid);
        $this->success('存储搜索记录成功', null, $getResult);
        exit;
      }
    }

    // 获取历史记录
    public function getHistory(){
      if($this->request->isPost()){
        $uid = $this->request->param('uid','','trim');
        $historyManager = new HistoryManager();
        $getResult = $historyManager->getAll($uid);
        $this->success('前十条历史记录', null, $getResult);
        exit;
      }
    }
    
    // 清除历史记录
    public function delHistory(){
      if($this->request->isPost()){
        $uid = $this->request->param('uid','','trim');
        $historyManager = new HistoryManager();
        $delResult = $historyManager->clear($uid);
        $this->success('清除历史记录成功', null, $delResult);
        exit;
      }
    }

    // 删除单条
    public function delOneHistory(){
      if($this->request->isPost()){
        $skey = $this->request->param('skey','','trim');
        $uid = $this->request->param('uid','','trim');
        // halt($skey);
        $historyManager = new HistoryManager();
        $delSingleResult = $historyManager->delete($uid,$skey);
        $this->success('删除单条成功', null, $delSingleResult);
        exit;
      }
    }

    //  获取分类标签
    public function getShopTab(){
      if($this->request->isPost()){
        // 查一级标签
        $tabResult = $this->classShopModel->where('parent_id','=',0)->select();
        $this->success('获取分类标签成功', null, $tabResult);
        exit;
      }
    }

    // 搜索结果
    public function searchKeyShop(){
      if($this->request->isPost()){
        $skey = $this->request->param('skey','','trim');
        $uid = $this->request->param('uid','','trim');
        $onekeyId = $this->classShopModel->where('name', 'like', '%' . $skey . '%')->value('id');
        $oneKeyRes = $this->classShopModel->where('parent_id','=' ,$onekeyId)->select();

        // halt($onekeyId);
        if($onekeyId != NULL){
          $towKeyList = $this->classShopModel->where('parent_id', $onekeyId)->select();
          $kids = [];
          foreach($towKeyList as $item){
            $kids[] = $item['id'];
          }
          // halt($kids);
          $twoKeyRes = $this->classShopModel->whereIn('parent_id',$kids)->select();
          // halt($twoKeyRes);
            if ($twoKeyRes) {
                $searchKeyData = [
                'oneKeyRes' => $oneKeyRes,
                'twoKeyRes' => $twoKeyRes,
                ];
                $this->success('获取分类结果成功',null,$searchKeyData);
                exit;
            }else{
                $searchKeyData = [
                'oneKeyRes' => NULL,
                'twoKeyRes' => NUll,
                ];
                $this->error('暂无改商品分类',null,$searchKeyData);
                exit;
            }
        }else{
            $searchKeyData = [
              'oneKeyRes' => NULL,
              'twoKeyRes' => NUll,
            ];
            $this->error('暂无改商品分类',null,$searchKeyData);
            exit;
        }
        
        
      }
    }
}



